package com.keyancompany.zhongbaopingtai.activity;

import android.support.v7.widget.Toolbar;
import android.text.Html;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.keyancompany.zhongbaopingtai.Constants;
import com.keyancompany.zhongbaopingtai.R;
import com.keyancompany.zhongbaopingtai.api.TeamAPI;
import com.keyancompany.zhongbaopingtai.model.BaseModel;
import com.keyancompany.zhongbaopingtai.model.Task;
import com.keyancompany.zhongbaopingtai.utils.T;

import java.util.List;

import butterknife.Bind;
import butterknife.BindString;

import static com.keyancompany.zhongbaopingtai.Constants.TEAM_CODE;

/**
 * @author xihe
 *         任务详情
 */
public class TaskDetailActivity extends BaseActivity {//没有周期，金额 状态完成

    @BindString(R.string.task_details)
    String projectTitle;
    @Bind(R.id.toolbar)
    Toolbar toolbar;


    @Bind(R.id.state)
    TextView state;
    @Bind(R.id.task_label)
    TextView taskLabel;
    @Bind(R.id.task_title)
    TextView taskTitle;
    @Bind(R.id.task_describe)
    TextView taskDescribe;
    @Bind(R.id.from_user)
    TextView fromUser;
    @Bind(R.id.to_user)
    TextView toUser;
    @Bind(R.id.groups)
    TextView groups;
    @Bind(R.id.task_start)
    TextView taskStart;
    @Bind(R.id.task_end)
    TextView taskEnd;
    private String taskId, teamCode;
    private int type;

    @Override
    protected int getContentViewId() {
        return R.layout.activity_task_detail;
    }

    @Override
    protected int getMenuId() {
        return 0;
    }

    @Override
    protected void initToolBar() {
        initToolBar(toolbar, projectTitle);
    }

    @Override
    protected void setUpViews() {
        taskLabel.setVisibility(View.GONE);
        taskStart.setVisibility(View.GONE);
        taskEnd.setVisibility(View.GONE);

        taskId = getIntent().getStringExtra(Constants.TASK_ID);
        teamCode = getIntent().getStringExtra(TEAM_CODE);
        type = getIntent().getIntExtra("type", 0);
        if (type == 0) {
            state.setText("待办中");
        } else if (type == 1) {
            state.setText("已过期");
        } else if (type == 2) {
            state.setText("进行中");
        } else if (type == 3) {
            state.setText("已完成");
        }
        initData();
    }


    private void initData() {
        if (isNetvaiable()) {

            TeamAPI.lookTask(teamCode, taskId, TaskDetailActivity.this,
                    getLoading(R.string.watting_hint));
        }
    }

    @Override
    public void success(int type, String json) {
        super.success(type, json);

        if (validateJson(json) != null) {
            BaseModel base = JSONObject.parseObject(json, BaseModel.class);
            if (base.result.equals(Constants.SUCESS)) {
                List<Task> look = JSONObject.parseArray(JSONObject.parseObject(base.data)
                        .getString("showtask"), Task.class);
                if (look.isEmpty()) {
                    return;
                } else {
                    Task item = look.get(0);
                    taskTitle.setText(item.getTitle());
                    taskDescribe.setText(Html.fromHtml(item.getTaskDis()));
                    fromUser.setText("发布者:" + item.getForname());
                    toUser.setText("指派:" + (TextUtils.isEmpty(item.getToname()) ? "未有指派人" : item.getToname()));
                    groups.setText("所在分组:" + item.getGroupName());
                }

            } else {
                T.showShort(mContext, base.msg);
            }
        }
    }

    @Override
    public void failed(String error) {
        super.failed(error);
        T.showShort(mContext, error);
    }

}
